package com.tjbklx1.redis.me;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisList {

	/**
	 * 保存key，并且设置过期时间
	 * 
	 * @param key
	 * @param time
	 *            秒
	 * @param value
	 */
	public static void lpush(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			jedis.lpush(key, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			RedisConnection.returnResource(pool, jedis);
		}
	}

	public static void rpush(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			jedis.rpush(key, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			RedisConnection.returnResource(pool, jedis);
		}
	}

	public static List<String> getAllList(String key) {
		List<String> value = new ArrayList<String>();
		if (StringUtils.isBlank(key)) {
			return value;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			value = jedis.lrange(key, 0, -1);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			RedisConnection.returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 获取list中最后一条
	 * 
	 * @param key
	 * @return
	 */
	public static String getListLastOne(String key) {
		String value = null;
		if (StringUtils.isBlank(key)) {
			return value;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			List<String> list = jedis.lrange(key, 0, 0);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			RedisConnection.returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 获取用户最后一个订单号
	 * 
	 * @author liuyuanfu 20141201
	 * @param key
	 * @return
	 */
	public static List<String> getLastOrder(String key) {
		List<String> value = new ArrayList<String>();
		if (StringUtils.isBlank(key)) {
			return value;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			value = jedis.lrange(key, 0, 0);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			RedisConnection.returnResource(pool, jedis);
		}
		return value;
	}
	
	/**
	 * 从List中删除指定的value
	 * 
	 * @param key
	 * @param value
	 */
	public static Long lrem(String key, String value) {
		Long ret = 0L;
		if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
			return ret;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			ret = jedis.lrem(key, 0, value); // 从List中移除所有指定value的内容
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return ret;
	}
	
	
	/**
	 * 在listName对应list的头部添加字符串元素
	 * 
	 * @param listName
	 * @param value
	 * @return
	 */
	public static long lpush(String listName, String... value) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			Long rank = jedis.lpush(listName, value);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return -1L;
	}

	/**
	 * 从key对应listName中删除count个和value相同的元素 count>0时，按从头到尾的顺序删除
	 * count<0时，按从尾到头的顺序删除 count=0时，删除全部
	 * 
	 * @param listName
	 * @param count
	 * @param value
	 * @return
	 */
	public static long lrem(String listName, long count, String value) {

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			Long rank = jedis.lrem(listName, count, value);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			RedisConnection.returnResource(pool, jedis);
		}
		return -1L;

	}

}
